package com.order.model.sys;  

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Model;

/** 
 * @Title: SysMenu.java 
 * @Package com.order.model.sys 
 * @Description: 后台系统菜单
 * @author LinBo
 * @date 2014-3-27 下午11:35:33 
 */

@TableBind(tableName="sys_menu", pkName="id")
public class SysMenu extends Model<SysMenu> {

	private static final long serialVersionUID = 1L;
	public static final SysMenu dao = new SysMenu();
	
	public static final String ID = "id";
	public static final String NAME = "name";
	public static final String URL = "url";
	public static final String SQ = "sq";
	public static final String SYS_MODULE_ID = "sys_module_id";
	public static final String LAST_UPDATED_BY = "last_updated_by";
	public static final String LAST_UPDATE_DATE = "last_update_date";
	public static String lastUpdatedUserName = "";
	
	/**加载系统模块，跟据用户拥有的子菜单来决定*/
	public List<SysMenu> getMenuByUserId(int userId) {
		String sql = " select m.name, m.id, m.sys_module_id, m.url , m.sq "
				+ " from sys_user_role ur, sys_role_menu rm, sys_menu m "
				+ " where ur.sys_role_id = rm.sys_role_id "
				+ " and rm.sys_menu_id = m.id "
				+ " and ur.sys_user_id = ? ";
		List<SysMenu> childrenMenu = dao.find(sql, userId);
		return childrenMenu;
	}


	/** 
	* @描述: 获取子节点，用于加载系统菜单
	*/ 
	public String getMenuByModuleId(int moduleId) {
		String sql = "select m.id,m.name,m.sys_module_id,m.url "
				+ " from sys_menu m where m.sys_module_id = ? order by m.sq";
		List<SysMenu> children_json = dao.find(sql, moduleId);
		StringBuffer j_s = new StringBuffer();
		if (children_json != null && children_json.size() > 0) {
			j_s.append(",\"children\":[");
			for (int i = 0; i < children_json.size(); i++) {
				j_s.append("{\"menuid\":\""	+ children_json.get(i).getNumber("ID").intValue() + "\",");
				j_s.append("\"url\":\""	+ children_json.get(i).getStr("URL").toString().trim() + "\",");
				j_s.append("\"text\":\""+ children_json.get(i).getStr("NAME").toString().trim()+ "\",");
				j_s.append("\"moduleid\":\""+ moduleId+ "\",");
				j_s.append("},");
			}
		} else {
			j_s.append("},");
		}
		if (j_s.toString().contains("children")) {
			j_s = new StringBuffer(j_s.substring(0, j_s.lastIndexOf(",")));
			j_s.append("]},");
		}
		return j_s.toString();
	}
	
	/**查询菜单信息，用于查询*/	
	public Map<String, Object> getMenuMessageByModuleId(int moduleId) {
		String sql = "select menu.*, module.name module_name "
					+ " from sys_menu menu,sys_module module "
					+ " 	where sys_module_id = ? " 
					+ " 	and module.id = ? ";
		List<SysMenu> list = dao.find(sql, moduleId, moduleId);
		list = getUpdatedBy(list);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("Rows", list);
		return map;
	}

	/**查询菜单信息，用于查询*/	
	public Map<String, Object> getMenuMessageByMenuId(int menuId) {
		String sql = "select menu.*, module.name module_name "
				+ " from sys_menu menu,sys_module module "
				+ " 	where sys_module_id = module.id " 
				+ " 	and menu.id = ? ";
		List<SysMenu> list = dao.find(sql, menuId);
		list = getUpdatedBy(list);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("Rows", list);
		return map;
	}


	/** 
	* @Title: getUpdatedBy 
	* @描述: 获取最后更新人姓名
	* @param list
	* @return   
	* List<SysMenu>    
	* @throws 
	*/ 
	public List<SysMenu> getUpdatedBy(List<SysMenu> list){
		if(list==null || list.isEmpty()){
			return list;
		}
		for(SysMenu menu : list){
			lastUpdatedUserName = SysUser.dao.findById(menu.get(LAST_UPDATED_BY)).getStr(SysUser.NAME);
			menu.put("last_updated_username", lastUpdatedUserName);
		}
		return list;
	}
	
	/** 
	* @Title: containMenu 
	* @描述: 判断是否存在对应id的菜单
	* @param menuid
	* @return   
	* boolean    
	* @throws 
	*/ 
	public static boolean containMenu(String menuid) {
		if(dao.findById(menuid) != null){
			return true;
		}
		return false;
	}
}
  